package day17.ticket;

public class SellTicket {
    public static void main(String[] args) {
        //多窗口售票问题
        //第一种
//        SellTicket01 sellTicket = new SellTicket01();
//        new Thread(sellTicket).start();
//        new Thread(sellTicket).start();
//        new Thread(sellTicket).start();
        //第二种
        SellTicket02 sellTicket02 = new SellTicket02();
        SellTicket02 sellTicket021 = new SellTicket02();
        sellTicket02.start();
        sellTicket021.start();

    }
}
//第一种实现方法
class SellTicket01 implements Runnable{
    private int ticketNum = 100;
    @Override
    public void run() {

        while (true) {
            if (ticketNum <= 0){
                System.out.println("售票结束...");
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("窗口 " +Thread.currentThread().getName() + " 售出一张票"
                    + " 剩余票数=" + (--ticketNum));
        }
    }
}
//第二种实现方法
class SellTicket02 extends Thread{
    private static int ticketNum = 100;//让多个线程共享 ticketNum
    @Override
    public void run() {
        while (true) {
            if (ticketNum <= 0){
                System.out.println("售票结束...");
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("窗口 " +Thread.currentThread().getName() + " 售出一张票"
                    + " 剩余票数=" + (--ticketNum));
        }
    }
}